﻿Imports MySql.Data.MySqlClient
Public Class DAEvento
    Public Function Salvar(ByVal objBEEvento As BE.BEEvento) As String
        Dim objBD As New BancoDeDados
        Dim strIdEvento As String = objBEEvento.IdEvento
        Try
            If objBEEvento.IdEvento = "" Then
                strIdEvento = objBD.ExecutaEscalar("INSERT INTO ext_evento (nome, url, status, data_inicio, data_fim, banner) VALUES ('" & objBEEvento.Nome & "', '" & objBEEvento.Banner & "', '" & objBEEvento.StatusEvento & "', '" & objBEEvento.DtInicio.ToString("yyyy-MM-dd") & "', '" & objBEEvento.DtFim.ToString("yyyy-MM-dd") & "', '" & objBEEvento.Banner & "')")
            Else
                objBD.ExecutaNonQuery("UPDATE ext_evento SET nome = '" & objBEEvento.Nome & "', banner = '" & objBEEvento.Banner & "', status = '" & objBEEvento.StatusEvento & "', data_inicio = '" & objBEEvento.DtInicio.ToString("yyyy-MM-dd") & "', data_fim = '" & objBEEvento.DtFim.ToString("yyyy-MM-dd") & "', banner = '" & objBEEvento.Banner & "' WHERE id_evento = '" & objBEEvento.IdEvento & "'")
            End If
            Return strIdEvento
        Catch ex As ApplicationException

            Throw New ApplicationException("extranet.DA.DAEvento.Salvar(): " & ex.Message)

        End Try
        Return strIdEvento
    End Function

    Public Function Listar() As List(Of BE.BEEvento)
        Dim objBD As New BancoDeDados
        Dim objDS As DataSet
        Dim lstBEEventos As New List(Of BE.BEEvento)
        objDS = objBD.ExecutaQuery("SELECT * FROM ext_evento")
        For Each objDR As DataRow In objDS.Tables(0).Rows
            Dim objBEEvento As New BE.BEEvento()
            objBEEvento.IdEvento = objDR("id_evento").ToString()
            objBEEvento.Nome = objDR("nome").ToString()
            objBEEvento.Banner = objDR("banner").ToString()
            objBEEvento.StatusEvento = objDR("status").ToString()
            objBEEvento.DtInicio = objDR("data_inicio").ToString()
            objBEEvento.DtFim = objDR("data_fim").ToString()
            lstBEEventos.Add(objBEEvento)
        Next
        Return lstBEEventos
    End Function
    Public Function ListarAtivos() As List(Of BE.BEEvento)
        Dim objBD As New BancoDeDados
        Dim objDS As DataSet
        Dim lstBEEventos As New List(Of BE.BEEvento)
        objDS = objBD.ExecutaQuery("SELECT * FROM ext_evento WHERE status = 'Ativo'")
        For Each objDR As DataRow In objDS.Tables(0).Rows
            Dim objBEEvento As New BE.BEEvento()
            objBEEvento.IdEvento = objDR("id_evento").ToString()
            objBEEvento.Nome = objDR("nome").ToString()
            objBEEvento.Banner = objDR("banner").ToString()
            objBEEvento.StatusEvento = objDR("status").ToString()
            objBEEvento.DtInicio = objDR("data_inicio").ToString()
            objBEEvento.DtFim = objDR("data_fim").ToString()
            lstBEEventos.Add(objBEEvento)
        Next
        Return lstBEEventos
    End Function
    Public Function ListarInativos() As List(Of BE.BEEvento)
        Dim objBD As New BancoDeDados
        Dim objDS As DataSet
        Dim lstBEEventos As New List(Of BE.BEEvento)
        objDS = objBD.ExecutaQuery("SELECT * FROM ext_evento")
        'objDS = objBD.ExecutaQuery("SELECT * FROM ext_evento WHERE status = 'Inativo'")
        For Each objDR As DataRow In objDS.Tables(0).Rows
            Dim objBEEvento As New BE.BEEvento()
            objBEEvento.IdEvento = objDR("id_evento").ToString()
            objBEEvento.Nome = objDR("nome").ToString()
            objBEEvento.Banner = objDR("banner").ToString()
            objBEEvento.StatusEvento = objDR("status").ToString()
            objBEEvento.DtInicio = objDR("data_inicio").ToString()
            objBEEvento.DtFim = objDR("data_fim").ToString()
            lstBEEventos.Add(objBEEvento)
        Next
        Return lstBEEventos
    End Function

    Public Function Selecionar(ByVal strIdEvento As Integer)
        Dim objBD As New BancoDeDados
        Dim objDATelaEvento As New DA.DATelasEvento
        Dim objDAInscritos As New DA.DAInscritos
        Dim objDS As DataSet
        Dim objBEEvento As New BE.BEEvento()
        objDS = objBD.ExecutaQuery("SELECT * FROM ext_evento WHERE id_evento = '" & strIdEvento & "'")
        If objDS.Tables(0).Rows.Count <> 0 Then
            objBEEvento.IdEvento = objDS.Tables(0).Rows(0)("id_evento").ToString()
            objBEEvento.Nome = objDS.Tables(0).Rows(0)("nome").ToString()
            objBEEvento.Banner = objDS.Tables(0).Rows(0)("banner").ToString()
            objBEEvento.StatusEvento = objDS.Tables(0).Rows(0)("status").ToString()
            objBEEvento.DtInicio = objDS.Tables(0).Rows(0)("data_inicio").ToString()
            objBEEvento.DtFim = objDS.Tables(0).Rows(0)("data_fim").ToString()
            objBEEvento.TelasEvento = objDATelaEvento.Listar(strIdEvento)
            objBEEvento.InscritosEvento = objDAInscritos.Listar(strIdEvento)
        End If
        Return objBEEvento
    End Function

    Public Sub Deletar(ByVal strIdEvento As String)
        Dim objBD As New BancoDeDados
        Try
            objBD.ExecutaNonQuery("DELETE FROM ext_evento WHERE id_evento = '" & strIdEvento & "'")
        Catch ex As ApplicationException
            Throw New ApplicationException("extranet.DA.DAEvento.Deletar(): " & ex.Message)
        End Try
    End Sub
End Class
